#User plugin
import DB

class login :
    db =  DB.DB( 
             "MySQLdb", 
             host="localhost",
             user="root",
             passwd="",
             db="blink",
             charset="tis620"
            );

    def __init__(self, user) :
        self.User = user
        self.User.events.addListener("login", self.login)
        self.uname = ""
        
    def login(self, evt) :
        xmlNode = evt.args["xmlNode"]
        self.uname = xmlNode.getAttribute("name") 
        
        evt.args["result"] = "pending"
        evt.args["reason"] = ""
        #d = login.db.runQuery("SELECT id, username, chips, golds, point,title, gender, class FROM users WHERE id='%(id)s' AND MD5(passhash)='%(pwd)s'" % {'id':uid,'pwd':pwd} )
        d = login.db.runQuery("SELECT count(*) FROM users")
        d.addCallback(self.logInResp)
        d.addErrback(self.logInErr)

    def logInResp(self,res) :
        if len(res)==1 : 
            self.User.acceptUser(self.uname)
        else :
            self.User.denyUser("Login failed!!!.")
            
    def logInErr(self,err) :
        self.User.denyUser("Database Error")
            
